package com.video.video.service.impl;

import com.video.common.utils.uuid.IdUtils;
import com.video.video.domain.app.dto.DGroupingDto;
import com.video.video.service.IDGoodsService;
import com.video.video.service.IDGroupItemService;
import java.util.List;
import com.video.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.video.video.mapper.DGroupingMapper;
import com.video.video.domain.DGrouping;
import com.video.video.service.IDGroupingService;

/**
 * 分组Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-29
 */
@Service
public class DGroupingServiceImpl implements IDGroupingService
{
    @Autowired
    private DGroupingMapper dGroupingMapper;
    @Autowired
    private IDGoodsService idGoodsService;
    @Autowired
    private IDGroupItemService idGroupItemService;


    /**
     * 查询分组
     *
     * @param id 分组主键
     * @return 分组
     */
    @Override
    public DGrouping selectDGroupingById(String id)
    {
        return dGroupingMapper.selectDGroupingById(id);
    }

    /**
     * 查询分组列表
     *
     * @param dGrouping 分组
     * @return 分组
     */
    @Override
    public List<DGrouping> selectDGroupingList(DGrouping dGrouping)
    {
        return dGroupingMapper.selectDGroupingList(dGrouping);
    }

    @Override
    public List<DGroupingDto> selectDGroupingDtoList(String userId) {
        List<DGroupingDto> list= dGroupingMapper.selectDGroupingDtoList();
        if(CollectionUtils.isNotEmpty(list)){
            for(DGroupingDto dto : list){
                //按照分组ID，查询分组下的商品
                dto.setGoodsList(idGoodsService.selectDGoodsDtoByGroupId(dto.getId(),userId));
            }
        }
        return list;
    }

    /**
     * 新增分组
     *
     * @param dGrouping 分组
     * @return 结果
     */
    @Override
    public int insertDGrouping(DGrouping dGrouping)
    {
        dGrouping.setId(IdUtils.getId());
        dGrouping.setCreateTime(DateUtils.getNowDate());
        return dGroupingMapper.insertDGrouping(dGrouping);
    }

    /**
     * 修改分组
     *
     * @param dGrouping 分组
     * @return 结果
     */
    @Override
    public int updateDGrouping(DGrouping dGrouping)
    {
        dGrouping.setUpdateTime(DateUtils.getNowDate());
        return dGroupingMapper.updateDGrouping(dGrouping);
    }

    /**
     * 批量删除分组
     *
     * @param ids 需要删除的分组主键
     * @return 结果
     */
    @Override
    public int deleteDGroupingByIds(String[] ids)
    {
        //删除分组下的商品
        idGroupItemService.deleteDGroupItemByGroupid(ids);
        return dGroupingMapper.deleteDGroupingByIds(ids);
    }

    /**
     * 删除分组信息
     *
     * @param id 分组主键
     * @return 结果
     */
    @Override
    public int deleteDGroupingById(String id)
    {
        return dGroupingMapper.deleteDGroupingById(id);
    }
}
